/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     bitStatusIcon.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "bitStatusIcon.h"
#include "levetop.h"
#include "bsp.h"
#include "uiIcon.h"

/***********************************************************************************
* Function Name : bit_control
* Description   : control variable's bit.
* Input			: - type: icon info sources
				  - buff: icon info
* Output        : None
* Return        : None
************************************************************************************/
void bit_control(uint8_t type, uint8_t *buff)
{
	uint16_t p_add, pointer;
	uint16_t x, y, w, h;
	uint16_t simage, eimage, nimage, d_num;
	uint8_t control_bit, Display_mode;
	uint8_t buff1[12] = {0};
	uint8_t Display_Mode = 0,flag;
	uint16_t overlay = 0;

	if (type == 0) // instruction information source is configuration file
	{
		Display_Mode = (buff[4] & 0x80) >> 7;
		p_add = (buff[5] + (buff[6] << 8));
		x = buff[7] + (buff[8] << 8);
		y = buff[9] + (buff[10] << 8);
		control_bit = buff[11];
		simage = buff[12] + (buff[13] << 8);
		eimage = buff[14] + (buff[15] << 8);
		Display_mode = buff[16];
		overlay = buff[17] + (buff[18] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		Display_Mode = (var[pointer] & 0x80) >> 7;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		x = var[pointer + 3] + (var[pointer + 4] << 8);
		y = var[pointer + 5] + (var[pointer + 6] << 8);
		control_bit = var[pointer + 7];
		simage = var[pointer + 8] + (var[pointer + 9] << 8);
		eimage = var[pointer + 10] + (var[pointer + 11] << 8);
		Display_mode = var[pointer + 12];
		overlay = var[pointer + 13] + (var[pointer + 14] << 8);
	}

	Var_AddrRemapping(&p_add);

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	nimage = (var[p_add * 2] << 8) + var[p_add * 2 + 1];

	if ((nimage & (0x01 << control_bit)) == 0)
		d_num = simage;
	else
		d_num = eimage;

	//LTPrintf("overlay %#x  \r\n",overlay);
	if(overlay == 0xFFFF)
		Display_Icon(d_num, x, y, Display_Mode);
	else
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * d_num + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		w = buff1[4] + (buff1[5] << 8);
		h = buff1[6] + (buff1[7] << 8);
		pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
		flag = buff1[11];

		if (pic_len != 0 && flag != 0xFF)
		{
				if (flag == 1 || flag == 2)
				{
					LT268_BG_to_buffer(0, 0, w, m_PngData,x, y,w, h);
				}

				if (pic_len != 0 && flag != 0xFF)
				{
					LT268_pic_to_buffer(0,0,w,m_PngData,
										0,0,w,pic_add,
										w,h,flag);
				}

				Get_date_variable_Id_overlay(pic_id,overlay,x,y,w,h);

				LT268_buffer_to_TFT(0,0,w,m_PngData,x,y,w,h);
		}
	}
}

void bit_control_to_buff(uint8_t type, uint8_t *buff,uint16_t overlay_x, uint16_t overlay_y, uint16_t overlay_w,uint16_t overlay_h)
{
	uint16_t p_add, pointer;
	uint16_t x, y, w, h;
	uint16_t simage, eimage, nimage,d_num;
	uint8_t control_bit, Display_mode;
	uint8_t buff1[12] = {0};
	uint8_t Display_Mode = 0,flag;
	uint16_t overlay = 0;

	if (type == 0) // instruction information source is configuration file
	{
		Display_Mode = (buff[4] & 0x80) >> 7;
		p_add = (buff[5] + (buff[6] << 8));
		x = buff[7] + (buff[8] << 8);
		y = buff[9] + (buff[10] << 8);
		control_bit = buff[11];
		simage = buff[12] + (buff[13] << 8);
		eimage = buff[14] + (buff[15] << 8);
		Display_mode = buff[16];
		overlay = buff[17] + (buff[18] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		Display_Mode = (var[pointer] & 0x80) >> 7;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		x = var[pointer + 3] + (var[pointer + 4] << 8);
		y = var[pointer + 5] + (var[pointer + 6] << 8);
		control_bit = var[pointer + 7];
		simage = var[pointer + 8] + (var[pointer + 9] << 8);
		eimage = var[pointer + 10] + (var[pointer + 11] << 8);
		Display_mode = var[pointer + 12];
		overlay = var[pointer + 13] + (var[pointer + 14] << 8);
	}
	
	Var_AddrRemapping(&p_add);

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	nimage = (var[p_add * 2] << 8) + var[p_add * 2 + 1];

	if ((nimage & (0x01 << control_bit)) == 0)
		d_num = simage;
	else
		d_num = eimage;

	LT_ReadFlash_UI(buff1, addr_index[10] + 12 * d_num + multi_language * 12 * addr_index[11], 12);
	pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
	w = buff1[4] + (buff1[5] << 8);
	h = buff1[6] + (buff1[7] << 8);
	pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
	flag = buff1[11];

	if (pic_len != 0 && flag != 0xFF)
	{
		x -= overlay_x;
		y -= overlay_y;

		LT268_pic_to_buffer(x ,y ,overlay_w,mix_ram,
									0,0,w,pic_add,
									w,h,flag);
	}
}
